﻿using System;
using System.IO;
using System.Linq;
using System.ServiceModel.Web;
using System.Text;
using BusinessLogic.Constant;
using ZeCloud.Resto55.Business;
using ZeCloud.Resto55.Business.Table;

namespace Resto55Business_WebRole
{
    // NOTE: If you change the class name "Resto55Service" here, you must also update the reference to "Resto55Service" in Web.config.
    public class Resto55Service : IResto55Service
    {
        #region IResto55Service Members
        private RestoService svc;

        public Resto55Service()
        {
            svc = BusinessFacade.CreerFacade().GetRestoService();
        }

        public Resto CreateResto55(string in_nom, int in_dep, string in_ville)
        {
            return this.CreateRestoFacade(in_nom, in_dep, in_ville);
        }

        public Resto[] GetRestoByQuery(string name)
        {
            WebOperationContext.Current.OutgoingResponse.Headers["Cache-Control"] = "no-cache";
            Resto[] result = BusinessFacade.CreerFacade().GetRestoService().RechercherRestosParVille(name).ToArray();
            return result;
        }

        public Element CreateElement(string uniqueRestoId, string in_element, string in_categorie, string in_description)
        {
            return this.CreateElementFacade(uniqueRestoId, in_element, in_categorie, in_description);
        }

        public DetailPrix CreateElementPrice(string in_elementId, double in_prix)
        {
            return CreateElementPriceFacade(in_elementId, in_prix);
        }

        public Element[] GetElementByResto(string in_restoId)
        {
            WebOperationContext.Current.OutgoingResponse.Headers["Cache-Control"] = "no-cache";
            Element[] elements = BusinessFacade.CreerFacade().GetRestoService().RechercherElementsPourResto(in_restoId).ToArray();
            return elements;
        }

        public DetailPrix[] GetDetailPrixByElementId(string in_elementId)
        {
            WebOperationContext.Current.OutgoingResponse.Headers["Cache-Control"] = "no-cache";
            DetailPrix[] prix = BusinessFacade.CreerFacade().GetRestoService().RechercherPrixPourElement(in_elementId).ToArray();
            return prix;
        }

        public Stream GetHelp()
        {
            MemoryStream mstream = new MemoryStream(Encoding.ASCII.GetBytes(WebService.Help));
            return mstream;
        }

        public bool CreateAllInOne()
        {
            try
            {
                string restoName = WebOperationContext.Current.IncomingRequest.Headers[HeaderValues.RestoName];
                string _restoDepartement = WebOperationContext.Current.IncomingRequest.Headers[HeaderValues.RestoDepartement];
                string restoVille = WebOperationContext.Current.IncomingRequest.Headers[HeaderValues.RestoVille];
                int restoDepartement = int.Parse(_restoDepartement);

                string elementName = WebOperationContext.Current.IncomingRequest.Headers[HeaderValues.ElementName];
                string elementDescription = WebOperationContext.Current.IncomingRequest.Headers[HeaderValues.ElementDescription];
                string elementCategorie = WebOperationContext.Current.IncomingRequest.Headers[HeaderValues.ElementCategorie];

                string _prix = WebOperationContext.Current.IncomingRequest.Headers[HeaderValues.ElementPrix];
                double prix = double.Parse(_prix, System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat);
                
                return this.CreateAllInOne(restoName, restoDepartement, restoVille, elementName, elementDescription, elementCategorie, prix);

            }
            catch (Exception ex)
            {
                WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.InternalServerError;
                WebOperationContext.Current.OutgoingResponse.StatusDescription = ex.Message;
                return false;
            }


        }

        public bool CreateWithParameter(string restoName, string departement, string ville, string elementName, string category, string description, string prix)
        {
            int _dep = int.Parse(departement);
            double _prix = double.Parse(prix);
            return this.CreateAllInOne(restoName, _dep, ville, elementName, category, description, _prix);
        }

        /// <summary>
        /// Creates a resto and an element and a price detail all in one request.
        /// </summary>
        /// <param name="restoName">The resto name to create or update.</param>
        /// <param name="restoDepartement">The departement of the resto.</param>
        /// <param name="restoVille">The city of the resto.</param>
        /// <param name="elementName">The element name.</param>
        /// <param name="elementDescription">The element description.</param>
        /// <param name="elementCategorie">The element category.</param>
        /// <param name="prix">A price detail to add to the element.</param>
        /// <returns>true if successfully created.</returns>
        private bool CreateAllInOne(string restoName, int restoDepartement, string restoVille, string elementName, string elementDescription, string elementCategorie, double prix)
        {
            // Step 1 : si le resto existe deja
            Resto resto = svc.GetRestoParNomEtVille(restoName, restoVille);
            // si null alors on le créé
            if (resto == null)
            {
                resto = this.CreateRestoFacade(restoName, restoDepartement, restoVille);
            }

            // Step 2 : on voit si l'element a creer existe deja
            Element element = svc.GetElementParNomEtResto(resto.uniqueId, elementName);

            if (element == null)
            {
                element = this.CreateElementFacade(resto.uniqueId, elementName, elementCategorie, elementDescription);
            }

            // Step 3 : on ajoute un detail de prix
            DetailPrix detailPrix = this.CreateElementPriceFacade(element.UniqueId, prix);

            return (detailPrix != null);

        }

        private Resto CreateRestoFacade(string in_nom, int in_dep, string in_ville)
        {
            Resto resto = BusinessFacade.CreerFacade().GetRestoService().CreerRestoAnonyme(in_nom, in_dep, in_ville);
            return resto;
        }

        private Element CreateElementFacade(string in_restoId, string in_element, string in_categorie, string in_description)
        {
            Element element = BusinessFacade.CreerFacade().GetRestoService().CreerElement(in_restoId, in_element, in_categorie, in_description);
            return element;
        }

        private DetailPrix CreateElementPriceFacade(string in_elementId, double in_prix)
        {
            DetailPrix prix = BusinessFacade.CreerFacade().GetRestoService().AjouterPrix(in_elementId, in_prix);
            return prix;
        }

        #endregion
    }
}
